<?php
/**
 * Created by PhpStorm.
 * Author: huangwy
 * Date: 2018/4/3 16:09
 * Version: 1.0
 */

namespace app\admin\controller;

use think\Db;
use app\admin\logic\CategoryLogic;

class Article extends Base
{
    public function add()
    {

        $categoryLogic = new CategoryLogic();
        $category = $categoryLogic->getCatListAttr();
        $category = $categoryLogic->getCatTree(0,$category);
        $this->assign('category', $category);//导航
        return view('form');
    }

    public function index($sid = 0)
    {
        $article = db('article');
        $pagesize = 1;#每页数量
        $prefix = config('DB_PREFIX');

        $sid = input('sid');
        $keyword = input('keyword');
        $order = input('order');
        $sid = isset($sid) ? $sid : '';
        $keyword = isset($keyword) ? htmlentities($keyword) : '';
        $order = isset($order) ? $order : 'DESC';
        $where = '1 = 1 ';
        if ($sid) {
            $sids_array = category_get_sons($sid);
            $sids = implode(',',$sids_array);
            $where .= "and a.sid in ($sids) ";
        }
        if ($keyword) {
            $where .= "and a.title like '%{$keyword}%' ";
        }
        //默认按照时间降序
        $orderby = "t desc";
        if ($order == "asc") {

            $orderby = "t asc";
        }
        //获取栏目分类
        $categoryLogic = new CategoryLogic();
        $category = $categoryLogic->getCatListAttr();
        $category = $categoryLogic->getCatTree($sid,$category);

        $this->assign('category', $category);//

        $list = $article->alias('a')
            ->join("{$prefix}category c"," c.id = a.sid")
            ->field("a.*,c.name")
            ->where($where)
            ->order($orderby)
            ->paginate($pagesize,false,['query'=>request()->param()]);

//        dump( Db::getLastSql());die;

        $this->assign('list', $list);
        return view();
    }

    public function del()
    {
        $aids = input('aids');
        $aids = isset($aids) ? $aids : false;
        if ($aids) {
            if (strpos($aids, ',')) {
//                $aids = implode(',', $aids);
                $map = [['aid','in',$aids],];
            } else {
                $map = 'aid=' . $aids;
            }
            if (db('article')->where($map)->delete()) {
                addlog('删除文章，AID：' . $aids);
                return json(['flag'=>0,'message'=>'恭喜，文章删除成功！']);
            } else {
                return json(['flag'=>1,'message'=>'参数错误！']);
            }
        } else {
            return json(['flag'=>1,'message'=>'参数错误！']);
        }

    }

    public function edit($aid)
    {

        $aid = intval($aid);
        $article = db('article')->where('aid=' . $aid)->find();
        if ($article) {

            //获取栏目分类
            $categoryLogic = new CategoryLogic();
            $category = $categoryLogic->getCatListAttr();
            $category = $categoryLogic->getCatTree($article['sid'],$category);
            $this->assign('category', $category);//导航

            $this->assign('article', $article);
        } else {
            $this->error('参数错误！');
        }
        return view('form');
    }

    public function update($aid = 0)
    {
        $info = input('post.');
        $aid = intval($info['aid']);
        $data['sid'] = isset($info['sid']) ? intval($info['sid']) : 0;
        $data['title'] = isset($info['title']) ? $info['title'] : false;
        $data['seotitle'] = isset($info['seotitle']) ? $info['seotitle'] : '';
        $data['keywords'] = $info['keywords'];
        $data['description'] = $info['description'];
        $data['content'] = isset($info['content']) ? $info['content'] : false;
        $data['thumbnail'] = $info['thumbnail'];
        $data['t'] = time();

        if ($aid) {
            $act = 'add';
        }else{
            $act = 'edit';
        }

        $validate = $this->validate($data, 'Article.'.$act, [], true);

        if ($validate !== true) {
            return json(['flag'=>1,'message'=>'警告！文章分类、文章标题及文章内容为必填项目。']);
        }
        if ($aid) {
            db('article')->data($data)->where('aid=' . $aid)->update();
            addlog('编辑文章，AID：' . $aid);
            return json(['flag'=>0,'message'=>'恭喜！文章编辑成功！']);
        } else {
            $aid = db('article')->data($data)->insert();
            if ($aid) {
                addlog('新增文章，AID：' . $aid);
                return json(['flag'=>0,'message'=>'恭喜！文章新增成功！']);
            } else {
                return json(['flag'=>1,'message'=>'抱歉，未知错误！']);
            }

        }
    }
}